home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ldb.zip / SBDRDEM2.CPP < prev    next >
C/C++ Source or Header  |  1991-10-18  |  4KB  |  192 lines

  1.     // sbdrdem2.cpp
  2.     // Demo Streamable Binder with streamable nodes
  3.     // Link with binder.obj and sbinder.obj
  4.     
  5.     #include <string.h>
  6.     #include <fstream.h>
  7.     #include <iomanip.h>
  8.     #include "sbinder.hpp"
  9.  
  10.     #define ID_StreamableString        3
  11.  
  12.     class StreamableString : Streamable  {
  13.         char * s;
  14.         int slen;
  15.     protected:
  16.         void construct(char *s, int slenNotClone);
  17.     public:
  18.         STREAMABLE(StreamableString,
  19.             ID_StreamableString,
  20.             Streamable);
  21.         StreamableString(char *s,
  22.             int slenNotClone = 0)
  23.             : Streamable(UNIQUE_STREAMABLE,
  24.                 ID_CLASS)
  25.             { construct(s,slenNotClone); }
  26.         ostream& printOn(ostream& os);
  27.         operator char *() { return s; }
  28.         virtual ~StreamableString()
  29.             { delete s; }
  30.     };
  31.     typedef StreamableString * StreamableStrinG;
  32.  
  33.  
  34.     ostream& StreamableString::store(ostream& os)
  35.     {
  36.         if (!(os << slen << endm))
  37.             serror("unable to store"
  38.                 "string length, id: ");
  39.         else if (slen)  {
  40.             os.write(s,slen);
  41.             if (!os)
  42.                 serror("unable to store"
  43.                 "string, id: ");
  44.         }
  45.         return os;
  46.     }
  47.  
  48.     StreamablE StreamableString::load(istream& is,
  49.         StreamablE InstancE)
  50.     {
  51.  
  52.         int slen;
  53.         char * s;
  54.  
  55.         if (!(is >> slen >> nextm))  {
  56.             lserror("loading slen",
  57.                 ID_CLASS);
  58.             return StreamablE0;
  59.         }
  60.         else if ((s = new char[slen+1])
  61.             != (char *)0)  {
  62.             if (slen)  {
  63.                 is.read(s,slen);
  64.                 if (!is)  {
  65.                     lserror("loading "
  66.                         "string",
  67.                         ID_CLASS);
  68.                     delete s;
  69.                     return StreamablE0;
  70.                 }
  71.             }
  72.             s[slen] = '\0';            
  73.         }
  74.         else  {
  75.             lserror("unable to allocate memory "
  76.                 "for string",ID_CLASS);
  77.             return StreamablE0;
  78.         }
  79.         if (!InstancE) if ((InstancE = (StreamablE)
  80.             new StreamableString
  81.             (UNIQUE_STREAMABLE)) == StreamablE0)  {
  82.             lserror("unable to construct"
  83.                 " StreamableString",
  84.                 ID_CLASS);
  85.             delete s;
  86.             return InstancE;
  87.         }
  88.         ((StreamableStrinG)InstancE)
  89.             ->construct(s,slen);
  90.         return InstancE;
  91.     }
  92.     
  93.     void StreamableString::construct(char *s,
  94.         int slenNotClone)
  95.     {
  96.         if (slenNotClone)  {
  97.             this->s = s;
  98.             slen = slenNotClone;
  99.         }
  100.         else if (s)
  101.             if ((this->s = strdup(s))
  102.                 != (char *)0)
  103.                 slen = strlen(s);
  104.             else
  105.                 slen = 0;
  106.         else  {
  107.             this->s = (char *) 0;
  108.             slen = 0;
  109.         }
  110.     }
  111.  
  112.     ostream& StreamableString::printOn(ostream& os)
  113.     {
  114.         if (s) if (strlen(s))
  115.             return os << s << endl;
  116.         else
  117.             return os << "empty string" << endl;
  118.         return os << "NULL string" << endl;
  119.     }
  120.  
  121.     void display(StreamableStrinG S)
  122.     {
  123.         cout << "Address: " << S
  124.             << "   String:   ";
  125.         S->printOn(cout);
  126.     }
  127.  
  128.  
  129.  
  130.     main()
  131.     {
  132.         SBinder::registerClass();
  133.         StreamableString::registerClass();
  134.  
  135.         SBinder B;
  136.  
  137.         B.push(new
  138.             StreamableString("Now is the time"));
  139.         B.insQ(new
  140.             StreamableString("for all programmers"));
  141.         B.atIns(B.Nodes(),new
  142.             StreamableString("to stop reinventing"));
  143.         B.insQ(new
  144.             StreamableString("the linked list!"));
  145.         B.insQ(B.bottom());
  146.         B.insQ(new
  147.             StreamableString("Line above for "
  148.                 "testing multiple linking!"));
  149.  
  150.         B.forEach((BDRforEachBlocK)display);
  151.         
  152.         B.link();  // required for streaming!
  153.  
  154.         cout << "\n\npress enter to continue ...";
  155.         cin.get();
  156.  
  157.  
  158.         ofstream oS("sbdrdem2.txt");
  159.         if (oS)  {
  160.  
  161.             oS << (StreamablE) B;
  162.             B.restream();
  163.             oS.close();
  164.             ifstream iS("sbdrdem2.txt");
  165.             if (iS)  {
  166.                 StreamablE C;
  167.                 iS >> C;
  168.                 iS.close();
  169.                 RestreamRegistry();
  170.                 if (C)
  171.                 {
  172.                   cout << "\n\nStreamed and "
  173.                     << "reloaded Binder "
  174.                     << "with streamable nodes \n\n";
  175.                   ((SBindeR)C)->forEach(
  176.                       (BDRforEachBlocK)display);
  177.                   delete (SBindeR) C;
  178.                 }
  179.                 else
  180.                   cout << "\n\nUnable to reload"
  181.                     << " Binder \n\n";
  182.             }
  183.             else
  184.                 cout << "\n\nUnable to reopen"
  185.                   << " stream for input of"
  186.                   << " of Binder \n\n";
  187.         }
  188.         
  189.         B.unlink();
  190.  
  191.         return 0;
  192.     }